<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Options extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();
	}
	function show($id)
	{

		$query="Select id,`idOption`,`Desc` from listoptions where idService=".$id;

		$result=$this->db->query($query);
		$responce->page=1;
		$i=0;
		foreach($result->result() as $row)
		{
			$query="Select name,rules from options where id=".$row->idOption;
			$this->log($query);
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$r2=$res->row();
				$responce->rows[$i]['id']="m".$row->id;
				$responce->rows[$i]['cell']=array($row->id,$r2->name,$row->Desc,$r2->rules);
				$i++;
			}
		}
		$this->db->close();
		return json_encode($responce);
	}
	function getList($type)
	{

		$query="Select id,name from options where idType=".$type;
		$this->log($query);
		$result=$this->db->query($query);
		$responce=array();
		foreach($result->result() as $row)
		{
			$responce[]=$row->id.":".$row->name;
		}
		if(count($responce)>0)
		{
			$responce= implode(";",$responce);
		}
		else {
			$responce="";
		}
		$this->db->close();
		return $responce;
	}

	function add($ServId,$name)
	{

		$query="Insert into listoptions(`idService`,`idOption`) values ('$ServId','$name')";
		$this->log($query);
		$this->db->query($query);
		$this->db->close();
	}

	function del($ServId,$id)
	{

		$id=substr($id,1);
		$query="Delete from listoptions where id=".$id;
		$this->log($query);
		$this->db->query($query);
		$this->db->close();
	}

	function edit($ServId,$Id,$desc)
	{

		$Id=substr($Id,1);
		$query="UPDATE listoptions SET `desc`='$desc' where id=".$Id;
		$this->log($query);
		$this->db->query($query);
		$this->db->close();
	}
}